import 'dart:convert';

import 'package:loxen/mode/address_mode.dart';
import 'package:loxen/mode/material/material_order_mode.dart';
import 'package:loxen/mode/operation_list_mode.dart';
import 'package:loxen/utils/app_enum_util.dart';

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class OrderDetailMode {
  int id;
  String time;
  String orderNumber;
  String mode;
  String state;
  String startTime;
  String endTime;
  Operation serviceCentre;
  Brand brand;
  List<Product> products;
  List<StateItem> status;
  Address address;
  Staff staff;

  String getState() {
    return OrderMap[state] ?? '未知状态';
  }

  String getMode() {
    return ModeMap[mode] ?? '未知状态';
  }

  OrderDetailMode({
    this.id,
    this.time,
    this.mode,
    this.state,
    this.startTime,
    this.endTime,
    this.serviceCentre,
    this.brand,
    this.products,
    this.status,
    this.address,
    this.staff,
    this.orderNumber,
  });

  factory OrderDetailMode.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    } else {
      List<Product> products;
      var address;
      products = asT<List>(jsonRes['product']) != null ? <Product>[] : null;
      if (products != null) {
        for (final dynamic item in jsonRes['product']) {
          products.add(Product.fromJson(item));
        }
      }
      List<StateItem> status;
      status = asT<List>(jsonRes['status']) != null ? <StateItem>[] : null;
      if (status != null) {
        for (final dynamic item in jsonRes['status']) {
          status.add(StateItem.fromJson(item));
        }
      }
      if (asT<String>(jsonRes['signer_name']) != null) {
        address = Address(
          signer_name: asT<String>(jsonRes['signer_name']),
          signer_mobile: asT<String>(jsonRes['signer_mobile']),
          area: Area.fromJson(asT<Map<String, dynamic>>(jsonRes['address']['area'])),
          address: asT<String>(jsonRes['address']['address']),
        );
      }
      return OrderDetailMode(
        id: asT<int>(jsonRes['id']),
        time: asT<String>(jsonRes['add_time']),
        mode: asT<String>(jsonRes['mode']),
        state: asT<String>(jsonRes['current_status']??jsonRes['status']),
        startTime: asT<String>(jsonRes['delivery_start_time']),
        endTime: asT<String>(jsonRes['delivery_end_time']),
        products: products,
        status: status,
        brand: Brand.fromJson(asT<Map<String, dynamic>>(jsonRes['brand'])),
        serviceCentre: Operation.fromJson(asT<Map<String, dynamic>>(jsonRes['service_centre'])),
        address: address,
        staff: Staff.fromJson(asT<Map<String, dynamic>>(jsonRes['staff'])),
        orderNumber: asT<String>(jsonRes['order_sn']),
      );
    }
  }
}

class StateItem {
  String time;
  String state;

  StateItem({this.time, this.state});

  factory StateItem.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : StateItem(
          time: asT<String>(jsonRes['add_time']),
          state: asT<String>(jsonRes['status']),
        );
}

class Staff {
  factory Staff.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : Staff(
          id: asT<int>(jsonRes['id']),
          name: asT<String>(jsonRes['nickname']),
          telephone: asT<String>(jsonRes['mobile']),
          mode: asT<String>(jsonRes['role_name']),
        );

  int id;
  String name;
  String telephone;
  String mode;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'id': id,
        'nickname': name,
        'signer_mobile': telephone,
      };

  @override
  String toString() {
    return json.encode(this);
  }

  Staff({this.id, this.name, this.telephone, this.mode});
}
